<?php

class Article extends Mysql {
	public function __construct() {
		parent::__construct();
	}
	/**
	 * 获得最新的num篇文章
	 */
	public function getLatestN($num) {
		$sql = "select * from article WHERE deleted='0' order by time desc LIMIT 0, $num;";
		return $this->exec_query($sql);
	}
	
	/**
	 * 通过id找一个文章
	 * 2012-5-3 下午4:55:54
	 * Xewee.Zhiwei.Wang
	 */
	public function queryById($id) {
		$sql = "select * from article where article.id = $id and deleted='0';";
		$rows = $this->exec_query($sql);
		if ($rows && count($rows) > 0) {//返回第一篇文章
			return $rows[0];
		}
		return $rows[0];
	}
	/**
	 * 根据文章type查询所有文章 分页
	 * 2012-5-2 上午10:49:26
	 * Xewee.Zhiwei.Wang
	 */
	public function query($articleTypeId, $pageIndex = 1, $pageSize = ARTICLE_PAGE_SIZE) {
		if (empty($pageIndex) || ltrim($pageIndex)=='') {
			$pageIndex = 1;
		}
		$pageIndex --;
		if (empty($pageSize) || ltrim($pageSize)=='') {
			$pageSize = ARTICLE_PAGE_SIZE;
		}
		$start = $pageIndex * $pageSize;
		$sql = "";
		//类型为空，则查询全部的
		if (empty($articleTypeId) || ltrim($articleTypeId)=='') {
			$sql = "select article.id, title, article.time, adminId, top, typeId, articletype.name, articletype.id as typeId from article join articletype on article.typeId = articletype.id where article.deleted = '0' order by article.top desc, article.time desc LIMIT $start, $pageSize;";
		}
		else {
			$sql = "select article.id, title, article.time, adminId, top, typeId, articletype.name, articletype.id as typeId from article join articletype on article.typeId = articletype.id where article.deleted = '0' and article.typeId = $articleTypeId order by article.top desc, article.time desc LIMIT $start, $pageSize;";
		}
		$rows = $this->exec_query($sql);
		return $rows;
	}
	
	public function count($articleTypeId) {
		$sql = "";
		//类型为空，则查询全部的
		if (empty($articleTypeId) || ltrim($articleTypeId)=='') {
			$sql = "select COUNT(article.id) as num FROM article where deleted = '0';";
		}
		else {
			$sql = "select COUNT(article.id) as num FROM article where deleted = '0' and typeId = $articleTypeId;";
		}
		$rows = $this->exec_query($sql);
		if ($rows && count($rows) > 0) {//返回第一个集合
			return $rows[0][num];//返回num字段
		}
		return 0;
	}
	/**
	 * 添加文章
	 * 2012-5-3 下午2:19:05
	 * Xewee.Zhiwei.Wang
	 */
	public function add($title, $content, $adminId, $typeId, $deleted, $top) {
		$sql = "insert into article(title, content, adminId, time, typeId, deleted, top) values ('$title', '$content', '$adminId', NOW(), $typeId, '$deleted', $top);";
		return $this->exec_update($sql);
	}
	
	/**
	 * 删除
	 * 2012-5-3 下午8:27:05
	 * Xewee.Zhiwei.Wang
	 */
	public function delete($id) {
		$sql = "update article set deleted = '1' where id = $id;";
		return $this->exec_update($sql);
	}
	
	public function setTop($id, $topValue) {
		$sql = "update article set top = '$topValue' where id = $id;";
		return $this->exec_update($sql);
	}
	
	public function update($id, $title, $content, $typeId, $top) {
		$sql = "update article set 
				title = '$title', 
				content = '$content', 
				typeId = $typeId, 
				top = '$top',
				deleted = '0'  
			where id = $id;";
		return $this->exec_update($sql);
	}
}

?>